/* -+- file  ./kf2/GUI/XmlConsole.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Habazie.kf2.Core;
using Habazie.kf2.Net;

namespace Habazie.kf2.GUI
{
	
	public class XmlConsole : Gtk.Window
	{
		protected Gtk.TextView m_View;
		private Gtk.TextTag m_In;
		private Gtk.TextTag m_Out;

		private Account m_Account;
		protected Gtk.CheckButton m_Enable;
		protected Gtk.TextView textview1;
		protected Gtk.HBox m_InputBox;
		protected Gtk.ToggleButton m_Insert;
		protected Gtk.TextView m_InputEditor;
		
		public XmlConsole(Account acc) : 
				base("")
		{
			m_Account = acc;
			Stetic.Gui.Build(this, typeof(Habazie.kf2.GUI.XmlConsole));
			Gtk.TextBuffer buffer = m_View.Buffer;
			this.Title = String.Format ("{0}: {1}", m_Account.Name, Title);
			
			m_In = new Gtk.TextTag ("in");
			m_In.Foreground = Util.Config.GetString ("color-in");
			buffer.TagTable.Add (m_In);
			
			m_Out = new Gtk.TextTag ("out");
			m_Out.Foreground = Util.Config.GetString ("color-out");
			buffer.TagTable.Add (m_Out);
			
			Enable ();
		}
		
		private void Enable ()
		{
			m_Account.OnReadText += TextRead;
			m_Account.OnWriteText += TextSent;
		}
		
		private void Disable ()
		{
			m_Account.OnReadText -= TextRead;
			m_Account.OnWriteText -= TextSent;
		}
		
		protected virtual void Insert (Gtk.TextTag tag, string str)
		{
			Gtk.TextIter i = m_View.Buffer.EndIter;
			m_View.Buffer.InsertWithTags (ref i, str, tag);
			m_View.Buffer.Insert (ref i, "\n");
		}
		
		protected virtual void TextRead (object o, string text)
		{
			Gtk.Application.Invoke (delegate {
				Insert (m_In, text);
			});
		}
		
		protected virtual void TextSent (object o, string text)
		{
			Gtk.Application.Invoke (delegate {
				Insert (m_Out, text);
			});
		}

		protected virtual void OnMInsertToggled(object sender, System.EventArgs e)
		{
			if (m_Insert.Active)
				m_InputBox.Show ();
			else
				m_InputBox.Hide ();
		}

		protected virtual void OnClearClicked(object sender, System.EventArgs e)
		{
			m_View.Buffer.Clear ();
		}

		protected virtual void OnDeleteEvent(object o, Gtk.DeleteEventArgs args)
		{
//			Disable ();
		}

		protected virtual void OnCloseClicked(object sender, System.EventArgs e)
		{
			this.Destroy ();
		}

		protected virtual void OnDestroyEvent(object o, Gtk.DestroyEventArgs args)
		{
			Disable ();
		}

		protected virtual void OnSendActivated(object sender, System.EventArgs e)
		{
			m_Account.Write (m_InputEditor.Buffer.Text);
		}
	}
	
}
